﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Stock.物品类型
{
    public partial class Category : Form
    {
        public Category()
        {
            InitializeComponent();
        }

        private void Category_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DBH.Select("select * from Category");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string CName = textBox1.Text;
            string sql = $"select * from Category where CName like '%{CName}%'";
            //取消自动列
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DBH.Select(sql);
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryAddAndEdit CA = new CategoryAddAndEdit();
            CA.f = this;
            CA.Show();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryAddAndEdit ca = new CategoryAddAndEdit();
            ca.Id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
            ca.CName = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            ca.f = this;
            ca.Show();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int Id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
                string sql = $"delete from Category where Id='{Id}'";
                if (DBH.NoSelect(sql) > 0)
                {
                    MessageBox.Show("删除成功！");
                    Category_Load(null, null);
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }
    }
    
}
